package cn.edu.bzu.ie.sf.mealsmisweb.controller;

import cn.edu.bzu.ie.sf.mealsmisweb.common.MyUtils;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.WeekRecipy;
import cn.edu.bzu.ie.sf.mealsmisweb.service.WeekRecipyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.controller
 * @Description : 周食谱控制器类
 * @Create on : 2021/8/15 18:43
 * @author: Feng Song 宋锋
 **/
@RestController
@RequestMapping(value = "/weekRecipy")
public class WeekRecipyController {
    @Autowired
    private WeekRecipyService weekRecipyService;

    /**
     * 获取某个部门下某个周的所有周食谱信息，用于大队用户的管理
     * @param pid 父部门id
     * @return List<WeekRecipy> 集合实例
     */
    @RequestMapping(value = "/getWeekRecipyListByPidAndWeek")
    public List<WeekRecipy> getWeekRecipyListByPidAndWeek(int pid){
        String year = MyUtils.getYearStrFromNow();
        int week = MyUtils.getWeekNumOfYear();
        return weekRecipyService.getWeekRecipyListByPidAndWeek(pid, year, week);
    }

    /**
     * 获取某个部门下的所有的
     * @param depid 部门id
     * @param pageNum 当前的页码
     * @param pageSize 每个页面的计算条数
     * @return PageResult 集合实例 分页信息
     */
    @RequestMapping(value = "/getWeekRecipyPageListByDepid")
    public Map<String, Object> getWeekRecipyPageListByDepid(int depid, int pageNum, int pageSize){
        Map<String, Object> map = new HashMap<>();
        long count = weekRecipyService.getCountByDepid(depid);
        map.put("total", count);
        List<WeekRecipy> list = weekRecipyService.getWeekRecipyPageListByDepid(depid, (pageNum-1)*pageSize, pageSize);  // 列表集合数据
        map.put("list", list);
        return map;
    }

    /**
     * 获取载个部门某个周的周食谱信息，用于中队用户的上传与维护
     * 也可以用于支队与大队用户的查询
     * @param depid 部门id
     * @return WeekRecipy 实例
     */
    @RequestMapping(value = "/getWeekRecipyByDepidAndWeek")
    public WeekRecipy getWeekRecipyByDepidAndWeek(int depid){
        String year = MyUtils.getYearStrFromNow();
        int week = MyUtils.getWeekNumOfYear();
        return weekRecipyService.getWeekRecipyByDepidAndWeek(depid, year, week);
    }

    /**
     * 添加周食谱信息
     * @param item 要添加的周食谱信息，无id信息
     * @return  WeekRecipy  带有id的实体
     */
    @PostMapping(value = "/addWeekRecipy")
    public WeekRecipy addWeekRecipy(WeekRecipy item){
        item.setWeek(MyUtils.getWeekNumOfYear());     // 当前所在的周数
        item.setWeekstr(MyUtils.getWeekStrOfNow());   // 时间段
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());   // 创建时间
        item.setYear(MyUtils.getYearStrFromNow());
        int iRow = weekRecipyService.addWeekRecipy(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 修改周食谱信息
     * @param item 要修改的周食谱信息
     * @return int 影响的行数
     */
    @PostMapping(value = "/editWeekRecipy")
    public  WeekRecipy  editWeekRecipy(WeekRecipy item){
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());   // 修改创建时间
        int iRow = weekRecipyService.editWeekRecipy(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 删除周食谱信息
     * @param id 要删除对象的id
     * @return int 影响的行数
     */
    @RequestMapping(value = "/delWeekRecipy")
    public int delWeekRecipy(int id){
        return weekRecipyService.delWeekRecipy(id);
    }
}
